<template>
  <div>
    <el-table v-loading="loading" :data="agentExpendList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="交易ID" align="center" prop="exchangeMark" />
      <el-table-column label="代理商名称" align="center" prop="agentName" />
      <el-table-column label="消费金额" align="center" prop="exchangeAmount" />
      <el-table-column label="消费后余额" align="center" prop="balance" />
      <el-table-column label="交易信息" align="center" prop="content" width="400"  />
      <el-table-column label="交易时间" align="center" prop="createTime" width="200"></el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listAgentExpend, getAgentExpend, delAgentExpend, addAgentExpend, updateAgentExpend } from "@/api/agentExpend/agentExpend";

export default {
  name: "AgentExpend",

  data() {
    return {
      queryinfo:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代理商消费明细表格数据
      agentExpendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        exchangeMark: [
          { required: true, message: "交易标识id，十位数字和字母组成不能为空", trigger: "blur" }
        ],
        storeId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        agentId: [
          { required: true, message: "代理商id不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "消费类型 0代理商充值 1商家充值 2商家发起背调扣费 3对应代理商接口调用扣费不能为空", trigger: "change" }
        ],
        exchangeAmount: [
          { required: true, message: "交易金钱数额不能为空", trigger: "blur" }
        ],
        balance: [
          { required: true, message: "交易后余额不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代理商消费明细列表 */
    getList() {
      this.loading = true;
      listAgentExpend(this.queryParams).then(response => {
        this.agentExpendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        exchangeMark: null,
        storeId: null,
        storeName: null,
        agentId: null,
        type: null,
        exchangeAmount: null,
        balance: null,
        content: null,
        dataId: null,
        imgUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理商消费明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAgentExpend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代理商消费明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAgentExpend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgentExpend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除代理商消费明细编号为"' + ids + '"的数据项？').then(function() {
        return delAgentExpend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('agentExpend/agentExpend/export', {
        ...this.queryParams
      }, `agentExpend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
